<?php

 
if (!defined('IN_IA')) {
    exit('Access Denied');
}

require MX_SHOP_PATH . 'core/inc/mroles.php';

class MrolesWebPage extends WebPage
{
    function ownerMenus()
    {
        global $_GPC, $_W;
        $routes = explode('.', $_W['routes']);
        $tab = isset($routes[0]) ? $routes[0] : '';

        include $this->template(  $tab . '/tabs');
    }

    /*
     * 获取创业者设备id
     * @param int $ismendian 0返回设备ids，1返回门店ids
     * @return array() $goodsid
     */
    public function getOwnerGoods($ismendian = 0){

        global $_W;
        $uid = $_W['manageid'];

        $owner = pdo_fetchall('SELECT * FROM '.tablename('mx_shop_service_goods_owner').' WHERE uniacid='.$_W['uniacid'].' AND uid=:uid AND deleted=0', array(':uid'=>$uid));
        $goodsid = array();
        if(!empty($owner)){
            foreach ($owner as $item) {
                $goodsid[] = $item['goods_id'];
            }
        }

        if(empty($ismendian)){
            return $goodsid;
        }

        $goodsid = implode(',', $goodsid);
        $goodsid = empty($goodsid) ? 0 : $goodsid;

        $goods = pdo_fetchall('SELECT mendian_id FROM '.tablename('mx_shop_service_goods').' WHERE uniacid='.$_W['uniacid'].' AND id in ('.$goodsid.') AND deleted=0 GROUP BY mendian_id');
        $mdids = array();
        if(!empty($goods)){
            foreach ($goods as $item) {
                $mdids[] = $item['mendian_id'];
            }
        }

        return $mdids;

    }

    /*
     * 获取门店设备id
     * @param int $type 1店主设备ids，2服务商设备ids
     * @param int $ismendian 0返回设备ids，1门店ids
     * @return array() $goodsid
     */
    public function getMendianGoods($type = 1, $ismendian = 0){

        global $_W;
        $uid = $_W['manageid'];

        if($type == 1){
            $condition = ' AND manageid=:uid';
        }else{
            $condition = ' AND channelid=:uid';
        }

        $params = array(':uid'=>$uid);

        $mendian = pdo_fetchall('SELECT id FROM '.tablename('mx_shop_mendian').' WHERE uniacid='.$_W['uniacid'].' AND deleted=0'.$condition, $params);
        if(empty($mendian)){
           return array();
        }

        $mdids = array();
        foreach ($mendian as $item) {
            $mdids[] = $item['id'];
        }

        if(!empty($ismendian)){
            return $mdids;
        }

        $mdids = implode(',', $mdids);

        $goods = pdo_fetchall('SELECT id FROM '.tablename('mx_shop_service_goods').' WHERE uniacid='.$_W['uniacid'].' AND mendian_id in ('.$mdids.') AND deleted=0');

        $goodsid = array();
        if(!empty($goods)){
            foreach ($goods as $item) {
                $goodsid[] = $item['id'];
            }
        }

        return $goodsid;

    }

    /*
     * 获取运营商设备id
     * @param int $ismendian 0返回设备ids，1门店ids
     * @return array() $goodsid
     */
    public function getOperatorGoods($ismendian = 0){

        global $_W;
        $uid = $_W['manageid'];

        $operator = pdo_fetchall('SELECT * FROM ' .tablename('mx_shop_operator') . ' WHERE uniacid='.$_W['uniacid'].' AND uid=:uid AND deleted=0', array(':uid'=>$uid));
        if(empty($operator)){
            return array();
        }

        $condpro= '';
        foreach ($operator as $item){
            $condpro .= ' or (province="'.$item['province'].'"';
            if($item['type'] == 2){
                $condpro .= ' AND city="'.$item['city'].'"';
                $condpro .= ' AND dist="'.$item['dist'].'"';
            }
            $condpro.=')';
        }

        $condpro = ltrim(ltrim($condpro, ' '),'or');
        $condition = 'status=1 AND deleted=0 AND ( '.$condpro.')';

        $mendian = pdo_fetchall('SELECT id FROM ' .tablename('mx_shop_mendian') . ' WHERE uniacid='.$_W['uniacid'].' AND '.$condition);
        if(empty($mendian)){
            return array();
        }

        $mdids = array();
        foreach ($mendian as $item) {
            $mdids[] = $item['id'];
        }

        if(!empty($ismendian)){
            return $mdids;
        }

        $mdids = implode(',', $mdids);

        $goods = pdo_fetchall('SELECT id FROM '.tablename('mx_shop_service_goods').' WHERE uniacid='.$_W['uniacid'].' AND mendian_id in ('.$mdids.') AND deleted=0');

        $goodsid = array();
        if(!empty($goods)){
            foreach ($goods as $item) {
                $goodsid[] = $item['id'];
            }
        }

        return $goodsid;

    }


}

/*
         * 获取令牌
         * @return string $token
         */
function getToken()
{
    global $_W,$_GPC;

    $long = mt_rand(12,24);

    $rand = random($long);

    $token = strtoupper(md5('__cgc_token_'.date('YmdH').'_'.$rand));

    $key = md5($token);
    isetcookie($key, $rand,7200);

    return $token;
}

/*
 * 检测令牌
 * @param string $token 需验证的token
 * @return bool
 */
function checkToken($token='')
{
    global $_W,$_GPC;

    if(empty($token))
    {
        return false;
    }

    $key = md5($token);
    if(empty($_GPC[$key]))
    {
        return false;
    }

    $tokent = strtoupper(md5('__cgc_token_'.date('YmdH').'_'.$_GPC[$key]));

    if($token != $tokent)
    {
        return false;
    }

    return true;
}
